package hp.com.dao.impl;

import hp.com.dao.BaseDao;
import hp.com.dao.ProductDAO;
import hp.com.dao.SukPropDao;
import hp.com.entity.Product;
import hp.com.entity.Suk_Options;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SukPropdaoImpl extends BaseDao implements SukPropDao {
    @Override
    public List<Suk_Options> getSUKOptionsByGoodId(int id) throws Exception {
        ProductDAO prod = new ProductDAOImpl();
        Product pdt = prod.GetProductById(id);
        //通过产品类型查看所有的属性
        String sql = "select prop_name,prop_id from good_prop where good=?";
        ResultSet rsAttr =this.executeQuery(sql,new Object[]{pdt.getId()});
        List<Suk_Options> list= new ArrayList<>();
        while(rsAttr.next()){
            Suk_Options suk = new Suk_Options();
            suk.setAttrId(rsAttr.getInt("prop_id"));
            suk.setAttr(rsAttr.getString("prop_name"));
            suk.setProp(getPropmapByPosisionID(suk.getAttrId()));;
            list.add(suk);
        }
        return list;
    }

    @Override
    public Map<Integer,String> getPropmapByPosisionID(int id) throws Exception {
        String sql = "select prop_num,prop_detail from prop_linking where prop_pos_id=?";
        ResultSet rs = this.executeQuery(sql,new Object[]{id});
        Map<Integer,String> map= new HashMap<>();
        while(rs.next()){
            map.put(rs.getInt("prop_num"),rs.getString("prop_detail"));
        }
        return map;
    }

    @Override
    public double GetSukPrice(String suk) throws Exception {
        String sql = "select suk_price from suk_table where good_suk=?";
        ResultSet rs=this.executeQuery(sql,new Object[]{suk});
        if (rs.next()){
            return rs.getDouble("suk_price");
        }
        return 0;
    }
}
